package com.yixun.qfbao.hander;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.DueRemainderConfigService;
import com.yixun.qfbao.apidto.UpdateDueRemainderConfigReqDto;
import com.yixun.qfbao.apidto.UpdateDueRemainderResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.DueRemainderConfigDto;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.DueRemainderConfigVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * User: zilong.liu
 * Date: 2020/7/15
 * Desc:
 *  修改或者更新消息到期提醒配置
 */
@Service
public class UpdateDueRemainderHander extends CommonAbstractHander {

    @Autowired
    DueRemainderConfigService dueRemainderConfigService;
    
    @Autowired
    CompanyService companyService;

    private static Logger logger= LoggerFactory.getLogger(UpdateCRHander.class);

    /**
     * 修改或者更新消息到期提醒配置
     * @param reqDto
     * @return
     */
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        DueRemainderConfigVo req = (DueRemainderConfigVo) reqDto;
        UpdateDueRemainderConfigReqDto updateDueRemainderConfigReqDto = req.getParams();

        DueRemainderConfigDto dueRemainderConfigDto = new DueRemainderConfigDto();
        dueRemainderConfigDto.setDueRemainderId(updateDueRemainderConfigReqDto.getDueRemainderId());
        dueRemainderConfigDto.setAdvanceTime(updateDueRemainderConfigReqDto.getAdvanceTime());
        dueRemainderConfigDto.setAdvanceTimeUnit(updateDueRemainderConfigReqDto.getAdvanceTimeUnit());
        dueRemainderConfigDto.setRemainderSwitch(updateDueRemainderConfigReqDto.getRemainderSwitch());
        dueRemainderConfigDto.setUpdateTime(new Date());
        dueRemainderConfigDto.setUpdateUserId(Long.valueOf(updateDueRemainderConfigReqDto.getUserId()));

        int result = 0;
        BaseResponseDTO<UpdateDueRemainderResDto> res = new BaseResponseDTO<UpdateDueRemainderResDto>();
        UpdateDueRemainderResDto updateDueRemainderResDto = new UpdateDueRemainderResDto();
        try {
            if(0 != updateDueRemainderConfigReqDto.getDueRemainderId()){
                //如果公司UUID为0，则是更新
                result = dueRemainderConfigService.updateDueRemainderConfig(dueRemainderConfigDto);
            }else{
                //插入
            	//先根据公司的UUID获取公司的ID
            	CompanyDto companyDto = new CompanyDto();
            	companyDto.setCompanyUuid(updateDueRemainderConfigReqDto.getCompanyUuid());
            	Company company = companyService.selectByDto(companyDto);
                dueRemainderConfigDto.setCompanyId(company.getId());
                dueRemainderConfigDto.setCreateUserId(Long.valueOf(updateDueRemainderConfigReqDto.getUserId()));
                dueRemainderConfigDto.setRemainderType(updateDueRemainderConfigReqDto.getRemainderType());
                dueRemainderConfigDto.setCreateTime(new Date());
                result = dueRemainderConfigService.insertDueRemainderConfig(dueRemainderConfigDto);
            }
        }catch (TServerException e){
            logger.error("UpdateDueRemainderHander error: {}", e);
            updateDueRemainderResDto.setResult(ResultEnum.FAIL.getNo());
            updateDueRemainderResDto.setResultMsg(e.getMessage());
        }catch (Exception e) {
            logger.error("UpdateDueRemainderHander error: {}", e);
            updateDueRemainderResDto.setResult(ResultEnum.FAIL.getNo());
            updateDueRemainderResDto.setResultMsg("修改失败!");
        }
        res.setResData(updateDueRemainderResDto);
        res.setResult(String.valueOf(result));
        return res;
    }
}
